//
// Created on 2025/11/18.
// Author 秦光磊.
#include "drawbusiness/draw_distribution.h"
#include "constant/constant.h"
#include "drawbase/draw_base.h"
#include "drawbridge/draw_component.h"
#include "function/my_function.h"
#include "render/egl_core.h"
#include "render/plugin_render.h"
#include <hilog/log.h>
#include <native_window/external_window.h>
#include <thread>
// 为全局变量分配内存
EGLCore *DrawDistribution::eglCoreForDistribute = nullptr;
DrawComponent *DrawDistribution::drawComponent = nullptr;
EGLDisplay DrawDistribution::eglDisplay = EGL_NO_DISPLAY;
EGLSurface DrawDistribution::eglSurface = EGL_NO_SURFACE;
GLuint DrawDistribution::program = 0;
float DrawDistribution::drawWidth = 0;
float DrawDistribution::drawHeight = 0;
float DrawDistribution::screenRatio = 1;
GLfloat DrawDistribution::distributeBackgroundColor[4] = {1.0f, 1.0f, 1.0f, 1.0f};
std::vector<int> DrawDistribution::electronDistributionData;
std::atomic<bool> running{true};

/**
 * 核外电子排布，绘制入口函数
 * @param env 代表当前执行环境
 * @param info 包含函数调用的上下文信息，包括传入的参数列表和数量，调用时的this值，与调用相关的数据指针
 * @return
 */
napi_value DrawDistribution::DrawDistributionBusiness(napi_env env, napi_callback_info info) {
    /**
     * 第一步，参数解析，解析出surfaceId、width、height
     */
    OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "DrawDistribution", "%{public}s,开始进行核外电子排布绘制。", __func__);
    // 解析参数，ARKTS获取XComponent的surfaceId
    int64_t surfaceId = 0;
    // ArkTS侧提供3个参数，surfaceId，width，height
    size_t argc = 3;
    napi_value args[3] = {nullptr};
    OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "DrawDistribution",
                 "%{public}s,开始使用napi_get_cb_info解析参数,ArkTS侧提供的参数，surfaceId，width，height.", __func__);
    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution",
                     "%{public}s,napi_get_cb_info fail,status:%{public}d.", __func__, status);
    }
    // 按照ArkTS侧提供的方法，第一个参数是surfaceId
    bool lossless = true;
    int index = 0;
    status = napi_get_value_bigint_int64(env, args[index++], &surfaceId, &lossless);
    if (status != napi_ok) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution",
                     "%{public}s,napi_get_value_bigint_int64 fail,status:%{public}d.", __func__, status);
    }
    // 按照ArkTS侧提供的方法，第二个参数是width
    double width;
    status = napi_get_value_double(env, args[index++], &width);
    if (status != napi_ok) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution",
                     "%{public}s,napi_get_value_double fail,status:%{public}d.", __func__, status);
    }
    // 按照ArkTS侧提供的方法，第三个参数是height
    double height;
    status = napi_get_value_double(env, args[index++], &height);
    if (status != napi_ok) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution",
                     "%{public}s,napi_get_value_double fail,status:%{public}d.", __func__, status);
    }
    OH_LOG_Print(LOG_APP, LOG_DEBUG, LOG_PRINT_DOMAIN, "DrawDistribution",
                 "%{public}s,解析完ArkTS侧提供的参数值，surfaceId:%{public}ld，width:%{public}f，height:%{public}f.",
                 __func__, surfaceId, width, height);
    /**
     * 第二步，基于XComponent的surfaceId创建nativeWindow
     */
    OHNativeWindow *nativeWindow;
    int32_t ret_create = OH_NativeWindow_CreateNativeWindowFromSurfaceId(surfaceId, &nativeWindow);
    if (ret_create != 0) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution",
                     "%{public}s,创建OHNativeWindow失败，返回值：%{public}d。", __func__, ret_create);
    }
    // 创建了窗口后，nativeWindow这个值后续的函数要用到，看怎么做下架构让这个值用起来方便点。
    /**
     * 第三步，渲染环境准备，这里输入参数是nativeWindow，加工后输出也是nativeWindow
     * 1，根据nativeWindow适配一个显示器eglDisplay
     * 2，根据nativeWindow创建一个eglWindow
     * 3，给eglDisplay、eglWindow适配一个eglSurface
     * 4，给eglDisplay创建一个上下文eglContext
     * 5，准备好显示器显示
     */
    // 使用EGLCore来进行核外电子排布分布
    eglCoreForDistribute = new EGLCore();
    drawComponent = new DrawComponent();
    // 创建渲染入口
    bool buildEntrance = eglCoreForDistribute->BuildRenderEntrance(nativeWindow, &eglDisplay, &eglSurface);
    if (!buildEntrance) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution",
                     "%{public}s,创建渲染入口失败，详见BuildRenderEntrance日志。", __func__);
        return 0;
    }
    bool buildProgram = eglCoreForDistribute->BuildShaderProgram(&program, width, height);
    if (!buildProgram) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution",
                     "%{public}s,着色器程序创建失败，详见BuildRenderEntrance日志。", __func__);
        return 0;
    }
    OH_LOG_Print(LOG_APP, LOG_DEBUG, LOG_PRINT_DOMAIN, "DrawDistribution", "%{public}s,width：%{public}f,height:%{public}f。",
                 __func__, width, height);
    eglCoreForDistribute->BuildViewport(&program, &drawWidth, &drawHeight, &screenRatio, width, height,
                                        distributeBackgroundColor,
                                        sizeof(distributeBackgroundColor) / sizeof(distributeBackgroundColor[0]));
    OH_LOG_Print(LOG_APP, LOG_DEBUG, LOG_PRINT_DOMAIN, "DrawDistribution",
                 "%{public}s,drawWidth：%{public}f,drawHeight:%{public}f。", __func__, drawWidth, drawHeight);
    electronDistributionData = MyFunction::GenElectronDistributionData(118);

    // 总体设计，整个画布，水平与垂直方向都拿出90%的区域来画图，避免某些曲面屏观察不方便，所以绘制区域的大小再次缩小
    float drawAreaScaleWidth = drawWidth * 9.0f / 10.0f;
    float drawAreaScaleHeight = drawHeight * 9.0f / 10.0f;
    // 总体起始x坐标，基于中心，在最左边
    float overallStartX = -drawAreaScaleWidth / 2.0f;
    // 总体起始z坐标为0
    float overallStartZ = 0.0f;

    // 上层高度占父层高度的10%
    float upPartHeight = drawAreaScaleHeight * 0.1f;
    // 上层y坐标,基于原点在正中心
    float upPartStartY = drawAreaScaleHeight / 2.0f - upPartHeight / 2.0f;
    // 绘制上层部分
    // drawUpPart(overallStartX, upPartStartY, overallStartZ, drawAreaScaleWidth, upPartHeight);

//    // 中层高度占父层高度的60%
//    float middlePartHeight = drawAreaScaleHeight * 0.6f;
//    // 中层y坐标,基于原点在正中心
//    float middlePartStartY = drawAreaScaleHeight / 2.0f - upPartHeight - middlePartHeight / 2.0f;
//    // 绘制中层部分
//    std::vector<GLfloat> middlePartStart = {overallStartX, middlePartStartY, overallStartZ};
////    DrawDistribution::DrawMiddlePart(eglCoreForDistribute, middlePartStart, middlePartStart.size(), drawAreaScaleWidth,
////                                     middlePartHeight);
    // 下层高度占父层高度的30%
    float downPartHeight = drawAreaScaleHeight * 0.3f;
    // 下层y坐标,基于原点在正中心
    float downPartStartY = -drawAreaScaleHeight / 2.0f + downPartHeight / 2.0f;
    // 绘制下层部分
    std::vector<GLfloat> downPartStart = {overallStartX, downPartStartY, overallStartZ, drawAreaScaleWidth,
                                          downPartHeight};
    bool isRendering = false;
    std::thread renderThread;
    std::atomic<bool> shouldStop{false};
    int targetFPS = 60;
    isRendering = true;
    shouldStop = false;

    
    

    
//    std::vector<GLfloat> startVector;
//    startVector.push_back(0.0f);
//    startVector.push_back(0.0f);
//    startVector.push_back(0.0f);
//    GLfloat drawColor[4] = {0.0f, 1.0f, 0.0f, 1.0f};
//    int geometryAttribute[2] = {2, 2};
//    drawComponent->DrawElectronDistributionParen(eglCoreForDistribute, startVector, 3, 1, 2, 8, drawColor, 4, 7, 5,
//                                                 geometryAttribute, 2);


///////////////////////////////////////////////////////////////////////////////////////////////
//    eglCoreForDistribute->EglContextInit2(nativeWindow, width, height);
///////////////////////////////////////////////////////////////////////////////////////////////
//


    return 0;
}

void renderLoop() {
    while (running) {
        // 总体设计，整个画布，水平与垂直方向都拿出90%的区域来画图，避免某些曲面屏观察不方便，所以绘制区域的大小再次缩小
        float drawAreaScaleWidth = DrawDistribution::drawWidth * 9.0f / 10.0f;
        float drawAreaScaleHeight = DrawDistribution::drawHeight * 9.0f / 10.0f;
        // 总体起始x坐标，基于中心，在最左边
        float overallStartX = -drawAreaScaleWidth / 2.0f;
        // 总体起始z坐标为0
        float overallStartZ = 0.0f;
        // 下层高度占父层高度的30%
        float downPartHeight = drawAreaScaleHeight * 0.3f;
        // 下层y坐标,基于原点在正中心
        float downPartStartY = -drawAreaScaleHeight / 2.0f + downPartHeight / 2.0f;
        // 绘制下层部分
        std::vector<GLfloat> downPartStart = {overallStartX, downPartStartY, overallStartZ, drawAreaScaleWidth,
                                              downPartHeight};
        DrawDistribution::DrawDownPart(DrawDistribution::eglCoreForDistribute, downPartStart, downPartStart.size(),
                                       drawAreaScaleWidth, downPartHeight);
        glFlush();
        glFinish();
        eglSwapBuffers(DrawDistribution::eglDisplay, DrawDistribution::eglSurface);
    }
}

bool RenderingDistributeOneFrame() {
    // 总体设计，整个画布，水平与垂直方向都拿出90%的区域来画图，避免某些曲面屏观察不方便，所以绘制区域的大小再次缩小
    float drawAreaScaleWidth = DrawDistribution::drawWidth * 9.0f / 10.0f;
    float drawAreaScaleHeight = DrawDistribution::drawHeight * 9.0f / 10.0f;
    // 总体起始x坐标，基于中心，在最左边
    float overallStartX = -drawAreaScaleWidth / 2.0f;
    // 总体起始z坐标为0
    float overallStartZ = 0.0f;
    // 下层高度占父层高度的30%
    float downPartHeight = drawAreaScaleHeight * 0.3f;
    // 下层y坐标,基于原点在正中心
    float downPartStartY = -drawAreaScaleHeight / 2.0f + downPartHeight / 2.0f;
    // 绘制下层部分
    std::vector<GLfloat> downPartStart = {overallStartX, downPartStartY, overallStartZ, drawAreaScaleWidth,
                                          downPartHeight};
    DrawDistribution::DrawDownPart(DrawDistribution::eglCoreForDistribute, downPartStart, downPartStart.size(),
                                   drawAreaScaleWidth, downPartHeight);
    glFlush();
    glFinish();
    eglSwapBuffers(DrawDistribution::eglDisplay, DrawDistribution::eglSurface);
    return true;
}


bool DrawDistribution::DrawDownPart(EGLCore *eglCore, std::vector<GLfloat> &downPartStart, int startSize,
                                    float downPartWidth, float downPartHeight) {
    OH_LOG_Print(LOG_APP, LOG_DEBUG, LOG_PRINT_DOMAIN, "DrawDistribution", "%{public}s start.", __func__);
    if (startSize < 3) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution",
                     "%{public}s param size error.startSize:%{public}d.", __func__, startSize);
        return false;
    }
    if (downPartStart.size() < startSize) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution",
                     "%{public}s downPartStart大小不对。downPartStart.size():%zu.", __func__, downPartStart.size());
        return false;
    }
    if (eglCore == nullptr) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution", "%{public}s eglCore为nullptr.",
                     __func__);
        return false;
    }
    OH_LOG_Print(LOG_APP, LOG_DEBUG, LOG_PRINT_DOMAIN, "DrawDistribution",
                 "%{public}s 长宽参数downPartWidth：%{public}f,downPartHeight:%{public}f", __func__, downPartWidth,
                 downPartHeight);
    if ((downPartWidth <= 0) || (downPartHeight <= 0)) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution",
                     "%{public}s 绘制区域长宽不能为0.downPartWidth：%{public}f,downPartHeight:%{public}f", __func__,
                     downPartWidth, downPartHeight);
        return false;
    }
    // 拆解起始坐标，便于编码
    float startX = downPartStart[0];
    float startY = downPartStart[1];
    float startZ = downPartStart[2];
    // 左右两边的宽度,该区域分为左右两边，左边占据10%，右边占据90%
    float leftWidth = downPartWidth * 0.1f;
    float rightWidth = downPartWidth * 0.9f;
    // 绘制右边的电子组态
    float rightStartX = startX + leftWidth;
    std::vector<GLfloat> configStartVector = {rightStartX, startY, startZ};
    DrawElectronConfiguration(eglCoreForDistribute, configStartVector, configStartVector.size(), rightWidth,
                              downPartHeight);
    return true;
}
bool DrawDistribution::DrawUpPart(EGLCore *eglCore, std::vector<GLfloat> &startVector, int startSize, float upPartWidth,
                                  float upPartHeight) {
    OH_LOG_Print(LOG_APP, LOG_DEBUG, LOG_PRINT_DOMAIN, "DrawDistribution", "%{public}s start.", __func__);
    if (startSize < 3) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution",
                     "%{public}s param size error.startSize:%{public}d.", __func__, startSize);
        return false;
    }
    if (startVector.size() < startSize) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution",
                     "%{public}s startVector大小不对。startVector.size():%zu.", __func__, startVector.size());
        return false;
    }
    if (eglCore == nullptr) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution", "%{public}s eglCore为nullptr.",
                     __func__);
        return false;
    }
    return true;
}

bool DrawDistribution::DrawMiddlePart(EGLCore *eglCore, std::vector<GLfloat> &middlePartStart, int startSize,
                                      float middlePartWidth, float middlePartHeight) {
    OH_LOG_Print(LOG_APP, LOG_DEBUG, LOG_PRINT_DOMAIN, "DrawDistribution", "%{public}s start.", __func__);
    if ((startSize < 3)) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution",
                     "%{public}s 参数检查不对。startSize:%{public}d.", __func__, startSize);
        return false;
    }
    if (middlePartStart.size() < startSize) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution",
                     "%{public}s middlePartStart大小不对。middlePartStart.size():%zu.", __func__,
                     middlePartStart.size());
        return false;
    }
    if (eglCore == nullptr) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution", "%{public}s eglCore为nullptr.",
                     __func__);
        return false;
    }
    OH_LOG_Print(LOG_APP, LOG_DEBUG, LOG_PRINT_DOMAIN, "DrawDistribution",
                 "%{public}s 参数值middlePartWidth：%{public}f,middlePartHeight:%{public}f.", __func__, middlePartWidth,
                 middlePartHeight);
    if ((middlePartWidth <= 0) || (middlePartHeight <= 0)) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution",
                     "%{public}s middlePartWidth与middlePartHeight不能为0.", __func__);
        return false;
    }
    // 拆解起始位置坐标，便于编码
    float startX = middlePartStart[0];
    float startY = middlePartStart[1];
    float startZ = middlePartStart[2];
    // 左右两边的宽度,该区域分为左右两边，左边占据10%，右边占据90%
    float leftWidth = middlePartWidth * 0.1f;
    float rightWidth = middlePartWidth * 0.9f;


    // 绘制右边的电子括号排布
    float rightStartX = startX + leftWidth;
    // geometryAttribute[0]为1表示虚线，为3表示实线
    // geometryAttribute[1]为1表示画小图，为2表示画大图
    int paren_geometryAttribute[] = {3, 2, 0, 0, 0};
    std::vector<GLfloat> startVector = {rightStartX, startY, startZ};
    DrawDistribution::DrawEnergySignOrDistributionViaParen(eglCore, startVector, startVector.size(), rightWidth,
                                                           middlePartHeight, 2, paren_geometryAttribute, 2);
    return true;
}

bool DrawDistribution::DrawElectronConfiguration(EGLCore *eglCore, std::vector<GLfloat> &configStartVector,
                                                 int startSize, float configAreaWidth, float configAreaHeight) {
    OH_LOG_Print(LOG_APP, LOG_DEBUG, LOG_PRINT_DOMAIN, "DrawDistribution", "%{public}s start.", __func__);
    if ((startSize < 3)) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution",
                     "%{public}s 参数检查不对。startSize:%{public}d.", __func__, startSize);
        return false;
    }
    if (configStartVector.size() < startSize) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution",
                     "%{public}s configStartVector大小不对。configStartVector.size():%zu.", __func__,
                     configStartVector.size());
        return false;
    }
    if (eglCore == nullptr) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution", "%{public}s eglCore为nullptr.",
                     __func__);
        return false;
    }
    OH_LOG_Print(LOG_APP, LOG_DEBUG, LOG_PRINT_DOMAIN, "DrawDistribution",
                 "%{public}s 参数值configAreaWidth：%{public}f,configAreaHeight:%{public}f.", __func__, configAreaWidth,
                 configAreaHeight);
    if ((configAreaWidth <= 0) || (configAreaHeight <= 0)) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution",
                     "%{public}s configAreaWidth与configAreaHeight不能为0.", __func__);
        return false;
    }
    // 拆解起始位置坐标，便于编码
    float startX = configStartVector[0];
    float startY = configStartVector[1];
    float startZ = configStartVector[2];
    // 两层的宽度都分为19格，每格将容纳一个亚层电子信息
    float cellBoxWidth = configAreaWidth / 19.0f;
    // 两层的高度都一样，占据40%
    float cellBoxHeight = configAreaHeight * 0.4f;

    // 绘制上层水平框
    // 起始x为startX,起始y为upBoxStartY,起始z为startZ
    float upBoxStartY = startY + configAreaHeight * 0.5f - cellBoxHeight * 0.5f;
    // geometryAttribute[0]为1表示虚线，为3表示实线
    // geometryAttribute[1]为1表示画小图，为2表示画大图
    int upBoxGeometryAttribute[] = {3, 1, 0, 0, 0};
    GLfloat drawColor[4] = {1.0f, 0.0f, 0.8f, 1.0f};
    drawComponent->DrawHorizontalBox(eglCoreForDistribute, configStartVector, configStartVector.size(), configAreaWidth,
                                     cellBoxHeight, 19, drawColor, 4, upBoxGeometryAttribute, 4);
    return true;
}

bool DrawDistribution::DrawEnergySignOrDistributionViaParen(EGLCore *eglCore, std::vector<GLfloat> &startVector,
                                                            int startSize, float drawAreaWidth, float drawAreaHeight,
                                                            int functionFlag, int *geometryAttribute, int attrSize) {
    OH_LOG_Print(LOG_APP, LOG_DEBUG, LOG_PRINT_DOMAIN, "DrawDistribution", "%{public}s start.", __func__);
    if ((startSize < 3) || (attrSize < 2)) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution",
                     "%{public}s 参数检查不对。startSize:%{public}d,attrSize:%{public}d.", __func__, startSize,
                     attrSize);
        return false;
    }
    if (startVector.size() < startSize) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution",
                     "%{public}s startVector大小不对。startVector.size():%zu.", __func__, startVector.size());
        return false;
    }
    if (eglCore == nullptr) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution", "%{public}s eglCore为nullptr.",
                     __func__);
        return false;
    }
    if (geometryAttribute == nullptr) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution", "%{public}s geometryAttribute为nullptr.",
                     __func__);
        return false;
    }
    OH_LOG_Print(LOG_APP, LOG_DEBUG, LOG_PRINT_DOMAIN, "DrawDistribution",
                 "%{public}s 参数值drawAreaWidth：%{public}f,drawAreaHeight:%{public}f.", __func__, drawAreaWidth,
                 drawAreaHeight);
    if ((drawAreaWidth <= 0) || (drawAreaHeight <= 0)) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "DrawDistribution",
                     "%{public}s drawAreaWidth与drawAreaHeight不能为0.", __func__);
        return false;
    }
    // 拆解起始坐标，方便编码
    float startX = startVector[0];
    float startY = startVector[1];
    float startZ = startVector[2];

    // 电子组态每个亚层占据的单元格长度，所得这个长度是为了计算每个括弧（一个括弧对应一个能级）的起始坐标，与实际的电子组态的能级对应
    float boxCellWidth = drawAreaWidth / 19.0f;
    // 1到7层，对应K\L\M\N\O\P\Q层
    for (int i = 1; i <= 7; i++) {
        // 总体思路，对应的层数总电子数大于0，则画括号，否则则不画括号
        // 当前能级总电子数初始化为0
        int levelElectronSum = 0;
        // 每个能级所占用的亚层序号，s亚层序号、p亚层序号、d亚层序号、f亚层序号，计算每次能级的电子数时，同步将当前亚层s、p、d、f对应的的19个轨道中的映射的序号也一并取出
        int subLayerSeq_s = 0;
        int subLayerSeq_p = 0;
        int subLayerSeq_d = 0;
        int subLayerSeq_f = 0;

        // 每次统计一个能级上所有亚层的电子总数时，从头到尾扫描一次19个亚层的电子分布信息，统计出该能级上的电子总数
        for (int j = 0; j < electronDistributionData.size(); j++) {
            // 此时先将起始亚层置为当前的亚层j
            // 找到与i能级对用的亚层信息来进行综合处理
            if (SUB_SHELL_ENERGY_LEVEL_SEQ[j] == i) {
                // 当前亚层轨道类型(s\p\d\f)与序号(j)的映射
                if (SUB_SHELL_ORBIT_TYPE[j] == "s")
                    subLayerSeq_s = j;
                if (SUB_SHELL_ORBIT_TYPE[j] == "p")
                    subLayerSeq_p = j;
                if (SUB_SHELL_ORBIT_TYPE[j] == "d")
                    subLayerSeq_d = j;
                if (SUB_SHELL_ORBIT_TYPE[j] == "f")
                    subLayerSeq_f = j;

                // 这里是累计当前能级下所有亚层电子总数
                levelElectronSum = levelElectronSum + electronDistributionData[j];
            }
        }

        // 能级一共会扫描7次（K\L\M\N\O\P\Q），每次扫描完后，若该能级的核外总电子数大于0，则绘制该能级的电子排布括弧表示形式
        if (levelElectronSum > 0) {
            // 先计算出当前能级的的x坐标
            float levelStartX;
            levelStartX = startX +
                          ((subLayerSeq_s + std::max(subLayerSeq_p, std::max(subLayerSeq_d, subLayerSeq_f))) / 2.0f) *
                              boxCellWidth -
                          boxCellWidth;
            // 绘制能级符号标志
            if (functionFlag == 1) {
                // 能级符号的起始位置
                float signStartX = levelStartX + boxCellWidth / 3.0f;
                // K能级的符号位置稍微调整一下
                if (i == 1)
                    signStartX = levelStartX + boxCellWidth * 0.6f;
                // 能级符号的尺寸
                float signScale = boxCellWidth * 0.8f;
//                drawCharacterAction.drawString(signStartX, startY, startZ, signScale, Constant.ENERGY_LEVEL_COLOR[i][0],
//                                               Constant.ENERGY_LEVEL_COLOR[i][1], Constant.ENERGY_LEVEL_COLOR[i][2],
//                                               Constant.ENERGY_LEVEL_COLOR[i][3], Constant.ENERGY_LEVEL_SIGN[i],
//                                               geometryAttribute);
            }
            // 电子分布括弧形式
            if (functionFlag == 2) {
                // 这里blocks参数填写8主要是为了增加美观，原则上应该填7，表示7个能级
                std::vector<GLfloat> startVector = {levelStartX, startY, startZ};
                int colorSize = COLOR_SIZE;

                GLfloat drawColor[4] = {ENERGY_LEVEL_COLOR[i][0], ENERGY_LEVEL_COLOR[i][1], ENERGY_LEVEL_COLOR[i][2],
                                        ENERGY_LEVEL_COLOR[i][3]};
                drawComponent->DrawElectronDistributionParen(eglCore, startVector, startVector.size(), boxCellWidth,
                                                             drawAreaHeight, 118, drawColor, colorSize, 8, i,
                                                             geometryAttribute, 2);
            }
        }
    }

    return true;
}